package com.atguigu.tingshu.order.pattern.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.order.pattern.TradeStrategy;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName : TrackTrade
 * Package : com.atguigu.tingshu.order.pattern.impl
 * Description :
 * <p>
 * Author: LHZ
 * Create: 2025/8/4 15:16
 * Version: 1.0
 */
@Slf4j
@SuppressWarnings("all")
@Component(SystemConstant.ORDER_ITEM_TYPE_TRACK)
public class TrackTrade implements TradeStrategy {

    @Autowired
    private AlbumFeignClient albumFeignClient;

    @Override
    public OrderInfoVo trade(Long userId, TradeVo tradeVo) {
        //创建返回结果对象
        OrderInfoVo orderInfoVo = new OrderInfoVo();
        //创建订单明细列表
        List<OrderDetailVo> orderDetailVoList = new ArrayList<>();
        //创建订单减免明细列表
        List<OrderDerateVo> orderDerateVoList = new ArrayList<>();
        //声明金额相关变量
        BigDecimal originPrice = new BigDecimal("0.00");
        BigDecimal discountPrice = new BigDecimal("0.00");
        BigDecimal derateAmount = new BigDecimal("0.00");

        //如果付款项目为声音
        //远程调用获取待购买声音列表
        List<TrackInfo> trackInfoList = albumFeignClient.findPaidTrackInfoList(tradeVo.getItemId(), tradeVo.getTrackCount()).getData();
        Assert.notNull(trackInfoList,"没有需要结算的声音");

        //调用远程方法获取专辑信息  从专辑信息中获取到单价-声音不支持折扣
        AlbumInfo albumInfo = albumFeignClient.getAlbumInfoById(trackInfoList.get(0).getAlbumId()).getData();
        Assert.notNull(albumInfo, "专辑{}不存在", trackInfoList.get(0).getAlbumId());
        BigDecimal price = albumInfo.getPrice(); //单价
        //计算相关价格数据
        originPrice = price.multiply(new BigDecimal(trackInfoList.size()));
        discountPrice = originPrice;

        //声明订单明细vo对象,加入到订单明细列表
        orderDetailVoList = trackInfoList.stream().map(o -> {
            OrderDetailVo orderDetailVo = new OrderDetailVo();
            orderDetailVo.setItemId(o.getId());
            orderDetailVo.setItemName("声音: " + o.getTrackTitle());
            orderDetailVo.setItemUrl(o.getCoverUrl());
            orderDetailVo.setItemPrice(price); //单价
            return orderDetailVo;
        }).collect(Collectors.toList());

        orderInfoVo.setItemType(tradeVo.getItemType()); //付款项目类型: 1001-专辑 1002-声音 1003-vip会员
        orderInfoVo.setOriginalAmount(originPrice);  //原价
        orderInfoVo.setDerateAmount(derateAmount);   //优惠价
        orderInfoVo.setOrderAmount(discountPrice);   //实际价
        orderInfoVo.setOrderDetailVoList(orderDetailVoList);
        orderInfoVo.setOrderDerateVoList(orderDerateVoList);

        return orderInfoVo;
    }
}